//
//  BaseViewController.m
//  Nidalee
//
//  Created by 李康 on 2020/11/30.
//  Copyright © 2020 李康. All rights reserved.
//

#import "BaseViewController.h"
#import "MJRefresh.h"

@interface BaseViewController ()
{
    UIButton *rightBtn;
    __weak UIScrollView *scrollView;
}
@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;
    [self initNavigationBar];
    [self initData];
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
-(void)initNavigationBar{
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: HexColor(0x333333), NSForegroundColorAttributeName, GetBoldFont(17),NSFontAttributeName, nil];
    [self.navigationController.navigationBar setBackgroundImage:[self createImageWithColor:HexColor(0xffffff)] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[self createImageWithColor:HexColor(0xe3e3e3)]];
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
}

-(UIImage*)createImageWithColor:(UIColor*)color{
    CGRect rect=CGRectMake(0.0f,0.0f,1.0f,1.0f);UIGraphicsBeginImageContext(rect.size);
    CGContextRef context=UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
    return theImage;
}

-(void)setNavTitle:(NSString *)title
{
    self.navigationItem.title = title;
    NSDictionary *dict = @{NSForegroundColorAttributeName : HexColor(0x333333)};
    [[UINavigationBar appearance] setTitleTextAttributes:dict];
}

-(void)addLeftImgBarButtonByItemImg:(NSString *)imgName
{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *leftImg = [UIImage imageNamed:imgName];
    leftBtn.frame = CGRectMake(0, 0, leftImg.size.width, leftImg.size.height);
    [leftBtn setImage:leftImg forState:UIControlStateNormal];
    
    [leftBtn addTarget:self action:@selector(leftBarAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
}

-(void)addLeftTextBarButtonByItemTitle:(NSString *)title
{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName: GetLightFont(16)}];
    leftBtn.frame = CGRectMake(0, 0, size.width, size.height);
    [leftBtn setTitle:title forState:UIControlStateNormal];
    [leftBtn setTitleColor:HexColor(0x333333) forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBarAction:) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.titleLabel.font = GetLightFont(16);
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
}

-(void)addRightImgBarButtonByItemImg:(NSString *)imgName
{
    rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *rightImg = [UIImage imageNamed:imgName];
    rightBtn.frame = CGRectMake(0, 0, rightImg.size.width, rightImg.size.height);
    [rightBtn setImage:rightImg forState:UIControlStateNormal];
    
    [rightBtn addTarget:self action:@selector(rightBarAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *containerView = [[UIView alloc] init];
    containerView.frame = rightBtn.frame;
    [containerView addSubview:rightBtn];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:containerView];
    self.navigationItem.rightBarButtonItem = rightItem;
}

-(void)addRightTextBarButtonByItemTitle:(NSString *)title
{
    rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName: GetLightFont(16)}];
    rightBtn.frame = CGRectMake(0, 0, size.width, size.height);
    [rightBtn setTitle:title forState:UIControlStateNormal];
    [rightBtn setTitleColor:HexColor(0x333333) forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBarAction:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.font = GetLightFont(16);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

-(void)addCenterView:(UIView *)view{
    self.navigationItem.titleView = view;
}


-(void)leftBarAction:(id)sender
{
    [self popViewController];
}

-(void)rightBarAction:(id)sender
{
    
}

-(void)initData
{
    //子类实现
}

-(void)initView
{
    self.view.backgroundColor = HexColor(0xFFFFFF);
    //子类实现
}


-(void)setExtraCellLineHidden:(UITableView *)tableView
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}

-(void)popViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(BOOL)popToViewController:(NSString *)vcName
{
    for(BaseViewController *vc in self.navigationController.viewControllers)
    {
        if([vc isKindOfClass:NSClassFromString(vcName)])
        {
            [self.navigationController popToViewController:vc animated:YES];
            return YES;
        }
    }
    return NO;
}

-(void)popToRootViewController
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)addPullRefreshToView:(UIScrollView *)view
{
    self.scrollView = view;
    self.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    
}

-(void)addLoadMoreToView:(UIScrollView *)view
{
    scrollView = view;
    mWeakSelf
    scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
    scrollView.mj_footer.automaticallyHidden = YES;
    MJRefreshBackNormalFooter *footer = (MJRefreshBackNormalFooter *)scrollView.mj_footer;
    [footer setTitle:@"上拉加载更多" forState:MJRefreshStateIdle];
    [footer setTitle:@"加载中..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"释放加载更多" forState:MJRefreshStatePulling];
}

-(void)beginRefresh
{
    if(scrollView && ![scrollView.mj_header isRefreshing])
    {
        [scrollView.mj_header beginRefreshing];
    }
}

-(void)endRefresh
{
    if (scrollView && [scrollView.mj_header isRefreshing])
    {
        [scrollView.mj_header endRefreshing];
    }
    else if (scrollView && [scrollView.mj_footer isRefreshing])
    {
        [scrollView.mj_footer endRefreshing];
    }
}

-(void)endLoadMore
{
    if (scrollView && [scrollView.mj_footer isRefreshing]) {
        [scrollView.mj_footer endRefreshing];
    }
}

-(BOOL)isRefreshing
{
    return [scrollView.mj_header isRefreshing];
}

//下拉刷新数据
-(void)refreshData
{
    
}
//加载更多数据
-(void)loadMoreData
{
    
}



@end
